let net = {
  fetch(url, method, param, header, start, success, compelete, exception) {

    let stream = weex.requireModule('stream')

    start()
    stream.fetch({
      method: method,
      url: url,
      type: 'json',
      headers: header,
      body: net.toParams(param)
    }, function (ret) {
      if (!ret.ok) {
        exception(ret)
      } else {
        let data={
          res:ret.data
        }
        success(data)
      }
    }, function (response) {
      console.log('get in progress:' + response.length);
    });
  },
  toParams(obj) {
    var param = ""
    for (const name in obj) {
      if (typeof obj[name] != 'function') {
        param += "&" + name + "=" + encodeURI(obj[name])
      }
    }
    return param.substring(1)
  },
  post(url, param, header, start, success, compelete, exception) {
    net.fetch(url, 'POST', param, header, start, success, compelete, exception)

  },
  postJson(url, param, header, start, success, compelete, exception) {
    net.fetch(url, 'POST', param, header, start, success, compelete, exception)
  },
  get(url, param, header, start, success, compelete, exception) {
    net.fetch(url, 'GET', param, header, start, success, compelete, exception)
  },
  postFile(url, param, header, path, start, success, compelete, exception) {
    let params = new FormData() // 创建form对象
    params.append('file', path.file) // 通过append向form对象添加数据
    params.append('type', param.type) // 添加for 

    console.log(header)
    var ajax = new XMLHttpRequest();

    ajax.open("POST", url, true);
    ajax.setRequestHeader("token", header.token);
    ajax.setRequestHeader("usersid", header.users_id);
    ajax.send(params);
    ajax.onreadystatechange = function () {
      if (ajax.readyState == 4) {
        if (ajax.status >= 200 && ajax.status < 300 || ajax.status == 304) {
          var obj = JSON.parse(ajax.responseText);
          success(obj);
        }
      }
    }

  },
  getSessionId(url) {

  },
  removeAllCookies() {

  },
  download(url, process, compelete, exception) {

  }

}
export default net